package hn.info.hockeynews.service;

import java.util.List;

import hn.info.hockeynews.dao.DaoMaster;
import hn.info.hockeynews.dao.DaoSession;
import hn.info.hockeynews.dao.Entry;
import hn.info.hockeynews.dao.EntryDao;
import hn.info.hockeynews.dao.MatchDao;
import hn.info.hockeynews.dao.DaoMaster.DevOpenHelper;
import hn.info.hockeynews.dao.MatchDao.Properties;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class DatabaseService extends Service {

	private long match_id;
	private DevOpenHelper helper;
	private DaoMaster daoMaster;
    private DaoSession daoSession;
    private MatchDao matchDao;
    private EntryDao entryDao;
    private Cursor cursor;
	private SQLiteDatabase db;
	private Entry e;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();
		
		if(e != null && helper != null){
			insertEntry(e, helper);
		}
	}

	/**
	 * insert a new entry in the database
	 * */
	public void insertEntry(Entry e, DevOpenHelper helper) {
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        entryDao = daoSession.getEntryDao();
        
        entryDao.insertOrReplace(e);
        
		Log.d("DB", "Added :"+e.getInfo()+"Time: "+e.getTimestamp()+" Data :"+e.toString()); 
	}
	
	public List<Entry> getEntryData(long id) {
		return entryDao.queryBuilder().where(Properties.Match_id.eq(id)).orderAsc(Properties.Match_id).list();
	}
	
	public long getMatch_id() {
		return match_id;
	}

	public void setMatch_id(long match_id) {
		this.match_id = match_id;
		System.out.println("ID was set to:"+match_id);
	}

	public DevOpenHelper getHelper() {
		return helper;
	}

	public void setHelper(DevOpenHelper helper) {
		this.helper = helper;
	}

	public Entry getE() {
		return e;
	}

	public void setE(Entry e) {
		this.e = e;
	}
}
